//
//
// adventure screen handler for the Sea Chest Object
//
// L. Dean Gibson II  3/20/01
//

#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adv_sea_chest.h"

#include "adv_object_model.h"
#include "adventure_frame.h"
#include "adventure_map.h"
#include "adventure_sounds.h"
#include "army.h"
#include "artifact_properties.h"
#include "artifact_type.h"
#include "basic_dialog.h"
#include "bitmap_layer_cache_window.h"
#include "bitmap_layer_cache_window_ptr.h"
#include "counted_ptr.h"
#include "default_artifacts.h"
#include "external_string.h"
#include "game_replace_keywords.h"
#include "hero.h"
#include "hero_keyword_replacer.h"
#include "materials.h"
#include "mouse_event.h"
#include "music.h"
#include "object_registration.h"
#include "player.h"
#include "random.h"
#include "replace_keywords.h"
#include "simple_dialog.h"

static t_object_registration<t_adv_sea_chest> k_registration( k_adv_object_treasure,
															  k_treasure_sea_chest );

// -------------------------------------------------------
// Sea Chest object constructor
// -------------------------------------------------------
t_adv_sea_chest::t_adv_sea_chest( std::string const& model_name )
			  : t_stationary_adventure_object( model_name )
{
	m_artifact = k_artifact_none;
}

// -------------------------------------------------------
// activate trigger for the Sea Chest adventure map object
// -------------------------------------------------------
void t_adv_sea_chest::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                                t_direction direction, t_adventure_frame* frame )
{
	t_player* player = army->get_owner();

	if (player == NULL)
		return;

	bool is_human = !army->get_owner()->is_computer();

	if (m_chest_gold == 0) { // we got an artifact here!
		t_artifact new_artifact;

		new_artifact = get_default_artifact(m_artifact);

		// give the decided artifact to the hero in the army, if any
		army->add(new_artifact);

		// display some sort of response 
		if (is_human)
		{
			t_basic_dialog*					dialog;
			std::string						text;
			t_bitmap_layer_cache_window_ptr icon_window;
			t_bitmap_layer_ptr				icon = get_icon( m_artifact );

			dialog = new t_basic_dialog;
			text = get_text( "Artifact" );
			text = replace_keywords(text, "%an_artifact", new_artifact.get_name());
			// add the artifact's name to the displayed text
			dialog->set_text( text );
			
			// add the icon to the window as well...
			icon_window = new t_bitmap_layer_cache_window( icon, t_screen_point(0,0), dialog, false );
			icon_window->set_help_balloon_text( new_artifact.get_name() );
			icon_window->set_right_click_text( new_artifact.get_help_text() );
			dialog->add_display_window( icon_window, new_artifact.get_name() );

			dialog->add_ok_button();
			dialog->set_title( get_name() );
			dialog->open( get_dialog_bottom( point ), true, k_align_bottom );
			t_sound_cache music = get_music_playing();
			stop_music();
			get_dialog_sound( k_dialog_sound_treasure )->play( get_sound_volume() );
			dialog->run_modal();
			play_music( music );
		}

	} else {
		player->gain(k_gold, m_chest_gold);
			
		if (is_human)
		{
			// display simple dialog of such
			t_basic_dialog*					dialog;
			std::string						text;
			t_bitmap_layer_cache_window_ptr icon_window;

			dialog = new t_basic_dialog;
            
            dialog->add_material( k_gold, m_chest_gold );
			text = get_text("gold");
			text = replace_keywords(text, m_chest_gold, k_gold);
			// add the artifact's name to the displayed text
			dialog->set_text( text );
			
			dialog->add_ok_button();

			dialog->open( get_dialog_bottom( point ), true, k_align_bottom );
			t_sound_cache music = get_music_playing();
			stop_music();
			get_dialog_sound( k_dialog_sound_treasure )->play( get_sound_volume() );
			dialog->run_modal();
			play_music( music );
		} 
	}
	destroy(); // remove us from the map

	// update the frame
	frame->update();

}

// -------------------------------------------------------
// Sea Chest as it is placed...
// -------------------------------------------------------
void t_adv_sea_chest::initialize( t_adventure_map& map )
{
	int die;

	// roll the die see how much gold, or if there is an item in this Sea Chest

	die = random(10);
	switch (die) {
	case 0:
	case 1:
	case 2:
		m_chest_gold = 1000;
		break;
	case 3:
	case 4:
	case 5:
		m_chest_gold = 1500;
		break;
	case 6:
	case 7:
	case 8:
		m_chest_gold = 2000;
		break;
	case 9:
		m_artifact = map.get_random_artifact(k_artifact_level_treasure);
		m_chest_gold = 0;
		break;
	default:
		m_chest_gold = 1000; // error! just make it some gold then...
		break;
	};
}

// -------------------------------------------------------
// Sea Chest as it appears on the adventure map
// -------------------------------------------------------
bool t_adv_sea_chest::write( std::streambuf& stream ) const
{
	put<t_artifact_type>( stream, m_artifact );
	put<int>( stream, m_chest_gold );
	return true;
}

// -------------------------------------------------------
// Sea Chest as it appears on the adventure map
// -------------------------------------------------------
bool t_adv_sea_chest::read( std::streambuf&						stream, 
						    t_qualified_adv_object_type const&	type,
						    int									version )
{
	m_artifact = get<t_artifact_type>( stream );
	m_chest_gold = get<int>( stream );
	return true;
}

// -------------------------------------------------------
// Treasure Chest as it appears on the adventure map
// -------------------------------------------------------
float t_adv_sea_chest::ai_value( t_adventure_ai const& ai, t_creature_array const& army, int move_cost ) const
{
	t_player* player = army.get_owner();
	
	return player->get_material_value(k_gold) * 1500 * 0.9f 
			+ player->ai_get_artifact_value(k_artifact_level_treasure) * 0.1f;
}


void t_adv_sea_chest::right_click( t_mouse_event const& event,
								   t_adventure_frame* adventure_frame )
{
	t_skill_mastery information = get_information_level();

	if (information < k_mastery_basic)
	{
		t_stationary_adventure_object::right_click( event, adventure_frame );
		return;
	}
	std::string				help_text = get_text( "scouting" );
	t_hero_keyword_replacer replacer;

	if (m_chest_gold > 0)
		replacer.add_material( m_chest_gold, k_gold );
	if (m_artifact != k_artifact_none)
		replacer.add_artifact( get_default_artifact( m_artifact ));
	show_popup_text( replacer( help_text ), event.screen_point );
}

